﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.Model;
using MeTalker.BLL;
public partial class Default_SoftManager : WebForm
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
            BindData();
        }
    }

    void BindData() {
        GridView1.DataKeyNames = new string[] { "AutoID" };
        List<SoftwaresMod> members = SoftwaresBLL.GetSoftwares();
        this.GridView1.DataSource = members;
        this.GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
        ShowMethod("EditItem", id);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            int id = int.Parse(e.CommandArgument as string);
            GridViewRow row = GridView1.Rows[id];
            id = int.Parse(GridView1.DataKeys[id].Values[0].ToString());
            if (SoftwaresBLL.Delete(id) > 0)
            {
                BindData();
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                SoftwaresMod mod = e.Row.DataItem as SoftwaresMod;
                if (mod != null)
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:EditItem(\"" + mod.AutoID + "\");return false;");
                    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：软件名称[" + mod.SoftName + "] 吗?')");
                }
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
}